package cn.woblog.recyclerviewsample.divider;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.State;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;
import cn.woblog.recyclerviewsample.R;

/**
 * Created by renpingqing on 17/4/6.
 */

public class GridItemDecoration extends RecyclerView.ItemDecoration {

  private final Context context;
  private final Drawable divider;
  private final Rect bounds = new Rect();

  public GridItemDecoration(Context context) {
    this.context = context;
    divider = context.getResources().getDrawable(R.drawable.item_divider);
  }

  @Override
  public void onDraw(Canvas c, RecyclerView parent, State state) {
    super.onDraw(c, parent, state);
//    drawVertical(c, parent, state);
//    drawHorizontal(c, parent, state);
    draw(c, parent);
  }

  //绘制横向 item 分割线
  private void draw(Canvas canvas, RecyclerView parent) {
    int childSize = parent.getChildCount();
    for (int i = 0; i < childSize; i++) {
      View child = parent.getChildAt(i);
      RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();

      //画水平分隔线
      int left = child.getLeft();
      int right = child.getRight();
      int top = child.getBottom() + layoutParams.bottomMargin;
      int bottom = top + divider.getIntrinsicHeight();
      divider.setBounds(left, top, right, bottom);
      divider.draw(canvas);

      //画垂直分割线
      top = child.getTop();
      bottom = child.getBottom() + divider.getIntrinsicHeight();
      left = child.getRight() + layoutParams.rightMargin;
      right = left + divider.getIntrinsicHeight();
//      if (mPaint != null) {
//        canvas.drawRect(left, top, right, bottom, mPaint);
//      }

      divider.setBounds(left, top, right, bottom);
      divider.draw(canvas);
    }
  }

  private void drawHorizontal(Canvas c, RecyclerView parent, State state) {
    int childCount = parent.getChildCount();
    int left = 0;
    int top = 0;
    int right = 0;
    int bottom = 0;

    for (int i = 0; i < childCount; i++) {
      View view = parent.getChildAt(i);

      parent.getLayoutManager().getDecoratedBoundsWithMargins(view, bounds);

      left = bounds.right; //item的右边，应为同一列有多个Item
      top = bounds.top; //item的顶部
      bottom = bounds.bottom;
      right = left + divider.getIntrinsicHeight();

      divider.setBounds(left, top, right, bottom);
      divider.draw(c);
    }
  }

  private void drawVertical(Canvas c, RecyclerView parent, State state) {
    int childCount = parent.getChildCount();
    int left = 0;
    int top = 0;
    int right = 0;
    int bottom = 0;

    for (int i = 0; i < childCount; i++) {
      View view = parent.getChildAt(i);

      parent.getLayoutManager().getDecoratedBoundsWithMargins(view, bounds);

      left = bounds.left; //item的左边，应为同一列有多个Item
      top = bounds.bottom; //item的底部
      bottom = top + divider.getIntrinsicHeight();
      right = bounds.right + divider.getIntrinsicHeight();

//      RecyclerView.LayoutParams params = (LayoutParams) view.getLayoutParams();
//
//      left=view.getLeft()+params.leftMargin;

      divider.setBounds(left, top, right, bottom);
      divider.draw(c);
    }
  }

  private int getSpanCount(RecyclerView parent) {
    // 列数
    int spanCount = -1;
    RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {

      spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {
      spanCount = ((StaggeredGridLayoutManager) layoutManager)
          .getSpanCount();
    }
    return spanCount;
  }

  @Override
  public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) {
//    int childAdapterPosition = parent.getChildAdapterPosition(view);
//    Log.d("TAG", childAdapterPosition + "");
//    outRect.set(0, 0, divider.getIntrinsicWidth(), divider.getIntrinsicWidth());
    int itemPosition = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition();
    int spanCount = getSpanCount(parent);
    int childCount = parent.getAdapter().getItemCount();

    boolean isLastRow = isLastRow(parent, itemPosition, spanCount, childCount);
    boolean isLastColumn = isLastColumn(parent, itemPosition, spanCount, childCount);

    int left;
    int right;
    int bottom;
    int eachWidth = (spanCount - 1) * divider.getIntrinsicHeight() / spanCount;
    int dl = divider.getIntrinsicHeight() - eachWidth;

    left = itemPosition % spanCount * dl;
    right = eachWidth - left;
    bottom = divider.getIntrinsicHeight();
    //Log.e("zzz", "itemPosition:" + itemPosition + " |left:" + left + " right:" + right + " bottom:" + bottom);
    if (isLastRow) {
      bottom = 0;
    }
    outRect.set(left, 0, right, bottom);
    Log.d("TAG", left + "，" + 0 + "," + right + "," + bottom);
  }

  private boolean isLastRow(RecyclerView parent, int pos, int spanCount,
      int childCount) {
    RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
      // childCount = childCount - childCount % spanCount;
      int lines = childCount % spanCount == 0 ? childCount / spanCount : childCount / spanCount + 1;
      return lines == pos / spanCount + 1;
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {
      int orientation = ((StaggeredGridLayoutManager) layoutManager)
          .getOrientation();
      // StaggeredGridLayoutManager 且纵向滚动
      if (orientation == StaggeredGridLayoutManager.VERTICAL) {
        childCount = childCount - childCount % spanCount;
        // 如果是最后一行，则不需要绘制底部
        if (pos >= childCount) {
          return true;
        }
      } else {
        // 如果是最后一行，则不需要绘制底部
        if ((pos + 1) % spanCount == 0) {
          return true;
        }
      }
    }
    return false;
  }

  private boolean isLastColumn(RecyclerView parent, int pos, int spanCount,
      int childCount) {
    RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
    if (layoutManager instanceof GridLayoutManager) {
      if ((pos + 1) % spanCount == 0) {// 如果是最后一列，则不需要绘制右边
        return true;
      }
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {
      int orientation = ((StaggeredGridLayoutManager) layoutManager)
          .getOrientation();
      if (orientation == StaggeredGridLayoutManager.VERTICAL) {
        if ((pos + 1) % spanCount == 0)// 如果是最后一列，则不需要绘制右边
        {
          return true;
        }
      } else {
        childCount = childCount - childCount % spanCount;
        if (pos >= childCount)// 如果是最后一列，则不需要绘制右边
        {
          return true;
        }
      }
    }
    return false;
  }
}
